/*
 * Visitors
 */

Visitors = function() {};

Visitors.prototype = {

  // general state
  parent: null,
  username: "anon",

  // ui elements
  ui_parent: null,
  ui_input: null,
  ui_list: null,
  ui_divname: 'visitors',
  ui_nick: null,
  ui_prev: null,

  // do layout in js for multiple instancing; creating or accepting a parent div
  layout: function(ui_parent) {
    this.ui_parent = ui_parent;
    return this;
  },

  // Build list of content
  database_update_all: function(all) {
    if(all.constructor != Array || all.length < 1) {
      return;
    }
    var str = "";
    var h = location.host.split('.');
    for(var i = 0; i < all.length; i++ ) {
      var element = all[i].elem;
      if(element && element.login ) {
        str += '<font color="green">'+element.login+'</font><br/><a href="http://'+element.login+'.'+h[1]+'.'+h[2]+'"><img src="'+element.depiction+'"/></a><br/>';
      } else {
        return; // xxx garbage is getting in....
      }
    }
    if( this.ui_prev == str ) return; // xxx sloppy
    this.ui_prev = str;
    this.ui_parent.innerHTML = str;
    return this;
  },

  database_update: function(elem) {
  },

  // startup
  //
  //  parent = div node for putting chat into
  //  uid = a unique place to store each particular conversation; up to the server to implement
  //  username = your current nickname
  //
  startup: function(parent,uid,username) {

    // div to print into
    if(parent == null) {
      return;
    }

    // guess at an initial nickname
    if( !username ) {
      username = 'anon';
    }

    // save state
    this.parent = parent;
    this.ui_nick = this.username = username;

    // perform an artistic layout procedurally
    this.layout(parent);

    // start synchronizing with the database - it calls local database_update() method eventually
    // visitors are not stored as notes; its a special kind of query case
    // we're interested in the root of this area so the path is ""
    DatabaseSubscribe( this, "", "table=visitors" );

    // all done setting up the chat area
    return this;
  }
}

// utility 
Visitors.make = function(parent,uid,username) {
  return ((new Visitors()).startup(parent,uid,username));
}

/*

 problems include;

    - since it is not the normal db; we cannot store properly in client db...
      this implies we need to respect specified table?

    - it is totally dynamic; so we cannot just do additions; we have to use the entire list...
      that means the database has to be less stupid... right now it is implicit by not having an id

    - the painting algorithm was incremental; it has to take the whole list...
      so it needs a start event...

*/



